package com.ls.fw.data.mapper.impl;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import com.ls.fw.data.bean.Record;
import com.ls.fw.data.mapper.ScanRowMapper;
import com.ls.fw.data.result.DataResultSet;
import com.ls.fw.data.utils.IDHander;


/**
 * 遍历数据映射适配器
 * @author ls
 * 2014年11月26日下午12:23:45
 */
public abstract class ScanRowMapperAdapter implements ScanRowMapper<Record>{

	private String pkName = "";
	
	
	public ScanRowMapperAdapter() {
		super();
	}
	
	
	/**
	 * 需要对主键特别处理
	 * @param pkName
	 */
	public ScanRowMapperAdapter(String pkName) {
		super();
		this.setPkName(pkName);
	}

	@Override
	public Record mapRow(DataResultSet result,int row) throws SQLException {
		return this.makeRecord(result);
	}
	
	@Override
	public boolean showField(int page){
		return true;
	}
	

	protected  Record makeRecord(DataResultSet resultSet)
			throws SQLException {
		Record record = null;
		record = new Record();
		ResultSetMetaData metaData = resultSet.getMetaData();
		int columnCount = metaData.getColumnCount();
		int index = 0;
		while (columnCount > index) {
			String name = metaData.getColumnName(index + 1);
			Object value = resultSet.getObject(index);
			if (!"".equals(pkName) && name.equals(pkName)) {
				String id = IDHander.resloveValue(String.valueOf(value));
				record.addField(name, id);
			}else{
				record.addField(name, value);
			}
			index++;
		}
		record.setSchemaName(resultSet.getSchemaName());
		record.setTable(resultSet.getTable());
		record.setVersion(resultSet.getVersion());
		record.setScore(resultSet.getScore());
		return record;
	}

	public String getPkName() {
		return pkName;
	}

	public void setPkName(String pkName) {
		this.pkName = pkName;
	}
}
